var accessControl = require('./accessControl');
var Post = require('../model/Post');

module.exports = function(app) {
  // http://localhost:3000/login
  app.get('/post', accessControl.checkLogin);
  app.get('/post', function(req, res) {
    res.render('post', {
      title: '发表',
      user: req.session.user,
      success: req.flash('success').toString(),
      error: req.flash('error').toString()
    });
  });

  // http://localhost:3000/login
  app.post('/post', accessControl.checkLogin);
  app.post('/post', function(req, res) {
    var currentUser = req.session.user;
    var post = new Post(currentUser.name, req.body.title, req.body.post);
    post.save(function(err) {
      if (err) {
        req.flash('error', err);
        return res.redirect('/');
      }
      req.flash('success', '发布成功！');
      res.redirect('/');
    });
  });
}